package com.android.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.android.R;
import com.android.view.adapter.base.CommonAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by TenFace
 * on 2017/9/4.
 */

public class PopSelectListAdapter extends CommonAdapter<String, RecyclerView.ViewHolder> {

    private int selectIndex = 0;

    public void setIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    @Override
    public RecyclerView.ViewHolder createVH(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_pop_select_list, parent, false);
        return new VH(view);
    }

    @Override
    public void bindVH(RecyclerView.ViewHolder viewHolder, final int position) {
        try {
            VH vh = (VH) viewHolder;
            if (position == selectIndex) {
                vh.tvContent.setTextColor(context.getResources().getColor(R.color.main_white));
            } else {
                vh.tvContent.setTextColor(context.getResources().getColor(R.color.main_button));
            }
            vh.tvContent.setText(data.get(position));
            vh.tvContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClick != null) {
                        itemClick.click(position, tag);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    static class VH extends RecyclerView.ViewHolder {

        @BindView(R.id.item_pop_select_list)
        TextView tvContent;

        public VH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private ItemClick itemClick;
    private int tag;

    public void setItemClickListener(ItemClick itemClick, int type) {
        this.itemClick = itemClick;
        this.tag = type;
    }

    public interface ItemClick {
        void click(int position, int type);

        void close();
    }

}
